﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace work_of_jump_100
{
    /// <summary>
    /// 游戏类
    /// </summary>
    class Game
    {
        private const int GameWidth = 700;
        private const int GameHeight = 800;
        Random ran = new Random();
        private List<Board> Boards { get; set; }

        /// <summary>
        /// 游戏自带的时钟
        /// </summary>
        private System.Timers.Timer Timer1 { get; set; }

        //定义委托
        public delegate void GameChanged();
        //定义事件
        public event GameChanged gameChanged;

        private List<Point> point;




        private Person _person;
        private Board _board;

        public Game()
        {
            int num;

            //初始化
            point = new List<Point>();
            Boards = new List<Board>();


            //随机生成板子
            for (int i = 1; i < 12; i++)
            {
                Point poi = new Point();
                poi.Y = i * 80;
                num = ran.Next(100, 511);
                poi.X = num;
                Rectangle rec = new Rectangle(poi, new Size(90, 20));
                Board board = new Board(rec);
                Boards.Add(board);
            }

            //定位人物初始位置
            _person = new Person(new Rectangle(350 - 50, 650 - 50, 70, 70));
            //产生一块初始板
            _board = new Board(new Rectangle(300, 650, 90, 20));



            //时钟
            Timer1 = new System.Timers.Timer();
            Timer1.Elapsed += Timer1_Elapsed;
            Timer1.Start();
            this.Timer1.Interval = 40;


        }



        private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Time.time++;
            //处理碰撞
            HandleTouch();
            //时钟处理程序
            Time.Y = (Time.time) ^ 2 + Time.time * 10 - 10;
            _person.Jump();




            //通知游戏改变
            gameChanged();
        }






        /// <summary>
        /// 处理碰撞
        /// </summary>
        private void HandleTouch()
        {
            foreach (var board in Boards)
            {
                {
                    if (
                        ((board.Rectangle.IntersectsWith(_person.Rectangle)) & (Time.SpeedY >= 0))
                        | ((_board.Rectangle.IntersectsWith(_person.Rectangle)) & (Time.SpeedY >= 0))
                        )
                    {
                        //人物和板撞上了
                        Time.time = 0;
                        Time.Y = 0;
                    }
                }


                {
                    if (board.Rectangle.IntersectsWith(_person.Rectangle) | (_board.Rectangle.IntersectsWith(_person.Rectangle)))
                    {
                        Flag.flag = true;
                    }
                    else
                    {
                        Flag.flag = false;
                    }
                }

            }


        }

        /// <summary>
        /// 绘制当前游戏场景
        /// </summary>
        /// <param name="g"> 绘图句柄</param>
        /// <param name="size">游戏区域尺寸</param>
        public void Draw(Graphics g, Size size)
        {
            //定义空白的图
            Image img = new Bitmap(GameWidth, GameHeight);

            Graphics _g = Graphics.FromImage(img);
            //定义一个矩形，长宽是预定义
            Rectangle rec = new Rectangle(new Point(0, 0), new Size(GameWidth, GameHeight));
            _g.DrawImage(Properties.Resources.bg, rec);

            //调用角色,板子
            _person.Draw(_g);
            _board.Draw(_g);


            foreach (var board in Boards)
            {
                board.Draw(_g);
            }


            //to do

            //绘制背景
            g.DrawImage(img, new Rectangle(new Point(0, 0), size));
        }

        // <summary>
        /// 处理键盘
        /// </summary>
        /// <param name="key"></param>
        public void KeyDown(String key)
        {
            _person.KeyDown1(key);

            _person.KeyDown2(key);

        }



    }
}
